import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import refresh from '@dfeidao/atom-web/url/refresh';
import get from '@dfeidao/atom-web/local/get';

export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// todo
	const no = get(fd, 'no');

	//工厂名称：
	const factory_name = fd.data.node.querySelector<HTMLInputElement>('[data-id="factory_name"]').value;
	if (!factory_name) {
		alert("请完善个人信息");
		return;
	}

	//工厂负责人：
	const factory_principle = fd.data.node.querySelector<HTMLInputElement>('[data-id="factory_principle"]').value;
	if (!factory_principle) {
		alert("请完善个人信息");
		return;
	}

	//负责人电话：
	const contacts_tel_one = fd.data.node.querySelector<HTMLInputElement>('[data-id="contacts_tel_one"]').value;
	if (!contacts_tel_one) {
		alert("请完善个人信息");
		return;
	}

	//工厂联系人：
	const contact = fd.data.node.querySelector<HTMLInputElement>('[data-id="contact"]').value;
	if (!contact) {
		alert("请完善个人信息");
		return;
	}

	//联系人电话：
	const contacts_tel_two = fd.data.node.querySelector<HTMLInputElement>('[data-id="contacts_tel_two"]').value;
	if (!contacts_tel_two) {
		alert("请完善个人信息");
		return;
	}

	//工厂地址：
	const factory_address = fd.data.node.querySelector<HTMLInputElement>('[data-id="factory_address"]').value;
	if (!factory_address) {
		alert("请完善个人信息");
		return;
	}


	const res1 = await nodejs<Array<{ factory_name: string }>>('company-factory/zj-000008/s001', {
		filter: {
			factory_no: no
		}
	});

	const arr: String[] = [];
	res1.forEach((v) => {
		arr.push(v.factory_name);
	});
	//console.log('zj7a003arr', arr);
	for (let i = 0; i < arr.length; i++) {
		if (factory_name === arr[i]) {
			alert("提交失败，数据已存在！");
			return;
		}
	}


	const res = await nodejs<{ code: 0 | 1; result: string }>('company-factory/zj-000008/s002', {
		filter: {
			factory_no: no,
			factory_name: factory_name,
			factory_principle: factory_principle,
			contacts_tel_one: contacts_tel_one,
			contact: contact,
			contacts_tel_two: contacts_tel_two,
			factory_address: factory_address
		}
	});

	if (res.code === 1) {
		alert('修改成功');
		refresh();
	} else {
		alert('修改失败');
		refresh();
	}
}
